<?php
/**
 *
 * @author Strawberry🍓
 * @date 2021-05-21 4:08 下午
 *
 */

namespace app\common\model\tang;


use app\common\impl\Comm;
use app\common\model\CommonBaseModel;
use Jrk\Tree;
class MonthSoleElectricityLog extends CommonBaseModel implements Comm
{


    public function getAdminPageData($param = [], $order = 'id desc')
    {
        // TODO: Implement getAdminPageData() method.
        $where=[];
        if (isset($param['merchant_id']) && $param['merchant_id']){
            $where[] = ["wm.merchant_id","=",$param["merchant_id"]];
        }
        if (isset($param['startDate']) && $param['startDate']){
            $where[] = ["wm.dataTime","=",$param["startDate"]];
        }
        if (isset($param['pointId']) && $param['pointId']){
//            $where[] = ["pointId","=",$param["pointId"]];
            $where[] = ['wm.pointId', 'like', "%" . $param['pointId'] . "%"];;
        }
        if (isset($param['code']) && $param['code']){
            $where[] = ["m.code","=",$param["code"]];
        }
        $data = $this
            ->alias('wm')
            ->leftjoin(['tang_merchant' => 'm'],"wm.merchant_id = m.id")
            ->leftjoin(['tang_organization_config' => 'c'],"m.organization = c.id")
            ->leftjoin(['tang_watch_maintain' => 'w'],"w.pointId = wm.pointId")
            ->where($where)
            ->order("wm.id desc")
//            ->page(PAGE)
//            ->limit(LIMIT)
            ->field("wm.*,m.merchant_sn,m.name,m.company,m.username,m.mobile,m.code,m.activities,c.name as organization,w.use_type")
            ->select()
            ->toArray();
        $count = count($data);
        //page??总页数
        $page = $param['page'];
        $limit = $param['limit'];
        $datas = array_splice($data, ($page - 1) * $limit, $limit);
        return parent::ajaxResult($datas, $count);
//        $data = self::where($where)->with(['merchant'])->order($order)->page(PAGE)->limit(LIMIT)->select()->toArray();
//        $count = self::where($where)->count("id");
//        return parent::ajaxResult($data, $count);
    }

    public function getExportPageData($param,$order='id desc',$where=[]){
        $where[] = ['w.is_normal', '<>', 2];;
        if (isset($param['merchant_id']) && $param['merchant_id']){
            $where[] = ["m.id","=",$param["merchant_id"]];
        }
        if (isset($param['code']) && $param['code']){
            $where[] = ["m.code","=",$param["code"]];
        }
        if (isset($param['startDate']) && $param['startDate']){
            $where[] = ["wm.dataTime","=",$param["startDate"]];
        }
        if (isset($param['pointId']) && $param['pointId']){
            $where[] = ['wm.pointId', 'like', "%" . $param['pointId'] . "%"];;
        }
        $data = $this
            ->alias('wm')
            ->leftjoin(['tang_merchant' => 'm'],"wm.merchant_id = m.id")
            ->leftjoin(['tang_organization_config' => 'c'],"m.organization = c.id")
            ->leftjoin(['tang_watch_maintain' => 'w'],"w.pointId = wm.pointId")
            ->where($where)
            ->order("wm.id asc")
//            ->page(PAGE)
//            ->limit(LIMIT)
            ->field("wm.*,m.merchant_sn,m.name,m.company,m.username,m.mobile,m.code,m.activities,c.name as organization,w.use_type")
            ->select()->toArray();
        $export = [];
        $title = ['商户名','合同号','组织','业态','使用类型','设备编号','设备名称','开始值','结束值','用量','单价','倍率','月份','是否核算'];

        foreach ($data as $key => $value) {
            if($value['name']=='-'||empty($value['name'])){
                $merchant_name = $value['company'];
            }else{
                $merchant_name = $value['name'];
            }
            if($value['use_type']==1) $use_type = '写字楼自用';
            if($value['use_type']==2) $use_type = '写字楼公区';
            if($value['use_type']==3) $use_type = '写字楼商户';
            if($value['use_type']==4) $use_type = '商场自用';
            if($value['use_type']==5) $use_type = '商场公区';
            if($value['use_type']==6) $use_type = '商场商户';
            if(empty($value['use_type'])){
                $use_type = '暂无配置';
            }
            $export[$key]['merchant_name'] =$merchant_name;
            $export[$key]['code'] =  "\t".$value['code']."\t";
            $export[$key]['organization'] =  "\t".$value['organization']."\t";
            $export[$key]['activities'] =  "\t".$value['activities']."\t";
            $export[$key]['use_type'] =  $use_type;
            $export[$key]['pointId'] =  "\t".$value['pointId']."\t";
            $export[$key]['pointName'] =  $value['pointName'];
            $export[$key]['zongStart'] =  $value['zongStart'];
            $export[$key]['zongEnd'] =  $value['zongEnd'];
            $export[$key]['totalPower'] =  $value['totalPower'];
            $export[$key]['price'] =  $value['price'];
            $export[$key]['rate'] =  $value['rate'];
            $export[$key]['dataTime'] =  $value['dataTime'];
            $export[$key]['status'] =  $value['status']==1?'已核算':'未核算';
        }
        return export_excel('单一用电明细_'.date('Y-m-d'),$title,$export);

    }

    public function merchant()
    {
        return $this->hasOne(Merchant::class,'id','merchant_id')->field('id,name,code,company');
    }
    public function delById($id)
    {
        // TODO: Implement delById() method.
        return parent::del($id);
    }

    public function doAll($data)
    {
        // TODO: Implement doAll() method.
        return parent::doAllData($data);
    }
}